import tool.*;

/**
 * @Description:
 * @Date:Create in 17:45  2022/5/11
 */
public class search0704 {
    public static void main(String[] args) {
        int nums[] = new int[]{-1,0,3,5,9,12};
        int target = 3;
        int i = search3(nums,target);
        System.out.println(i);
    }
    public static int search1(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while(left<=right){
            int mid = left + ((right-left)/2);
            if(nums[mid]<target){
                left = mid + 1;
            }else if(nums[mid]>target){
                right = mid - 1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    public static int search2(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1; // 定义target在左闭右闭的区间里，[left, right]
        while(left<=right){// 当left==right，区间[left, right]依然有效，所以用 <=
            int mid = left + ((right-left)/2);
            if(nums[mid]<target){
                left = mid + 1;
            }else if(nums[mid]>target){
                right = mid - 1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    public static int search3(int[] nums, int target) {
        int left = 0;
        int right = nums.length; // 定义target在左闭右闭的区间里，[left, right）
        while(left<right){ // 因为left == right的时候，在[left, right)是无效的空间，所以使用 <
            int mid = left + ((right-left)/2);
            if(nums[mid]<target){
                left = mid + 1;
            }else if(nums[mid]>target){
                right = mid;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
